/**
 *
 */
package org.suren.littlebird.actions.bundle;

import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableItem;
import org.suren.littlebird.Activator;
import org.suren.littlebird.actions.Reload;
import org.suren.littlebird.po.BundleStatistic;
import org.suren.littlebird.po.SuRenBundle;
import org.suren.littlebird.preferences.PreferenceConstants;
import org.suren.littlebird.server.BundleServer;
import org.suren.littlebird.server.BundleServerFactory;
import org.suren.littlebird.views.provider.SuRenBundleFieldProvider;

/**
 * @author suren
 *
 */
public class SuRenBundleReload implements Reload {
	private TableViewer viewer;
	private IPreferenceStore store;
	private IStatusLineManager statusLineMgr;
	private BundleStatistic bundleStatistic;
	private Label statusLabel;

	/**
	 * @param viewer
	 * @param iStatusLineManager
	 * @param statusLabel
	 */
	public SuRenBundleReload(TableViewer viewer,
			IStatusLineManager iStatusLineManager, Label statusLabel) {
		this.store = Activator.getDefault().getPreferenceStore();
		this.viewer = viewer;
		this.statusLineMgr = iStatusLineManager;
		this.statusLabel = statusLabel;
	}

	@Override
	public void reload(Object source, Object data)
	{
		new Job("Bundle List Reloading"){

			@Override
			protected IStatus run(IProgressMonitor arg0)
			{
				BundleServer bundleServer = BundleServerFactory.getInstance();
				String filter = store.getString(PreferenceConstants.P_OSGI_FILTER);
				final List<SuRenBundle> bundles;

				if(filter == null || "".equals(filter))
				{
					bundles = bundleServer.getAll();
				}
				else
				{
					bundles = bundleServer.searchBy(filter);
				}

				bundleStatistic = new BundleStatistic();
				bundleStatistic.setTotalNum(bundles.size());
				for (SuRenBundle bundle : bundles) {
					switch (bundle.getState()) {
						case SuRenBundle.ACTIVE:
							bundleStatistic.increaseActive();
							break;
						case SuRenBundle.INSTALLED:
							bundleStatistic.increaseInstalled();
							break;
						case SuRenBundle.RESOLVED:
							bundleStatistic.increaseResolved();
							break;
					}
				}

				Display.getDefault().syncExec(new Runnable()
				{

					@Override
					public void run()
					{
						viewer.setInput(bundles);

						statusReload();
					}
				});

				return Status.OK_STATUS;
			}
		}.schedule();
	}

	private Color getColor(TableItem item)
	{
		String state = item.getText(3);
		Display display = Display.getDefault();
		int color = -1;

		if (SuRenBundleFieldProvider.RESOLVED.equals(state)) {
			color = SWT.COLOR_YELLOW;
		} else if (SuRenBundleFieldProvider.INSTALLED.equals(state)) {
			color = SWT.COLOR_GRAY;
		} else if (SuRenBundleFieldProvider.UNKNOW.equals(state)) {
			color = SWT.COLOR_RED;
		}

		if (color != -1) {
			return display.getSystemColor(color);
		} else {
			return null;
		}
	}

	private void statusReload()
	{
		String osgiServer = store.getString(PreferenceConstants.P_OSGI_SERVER);
		statusLineMgr.setMessage("OsgiServer : " + osgiServer);

		statusLabel.setText(bundleStatistic.toString());
	}
}
